package com.sokal.easycare.modules.kpoint.service.activity;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.sokal.easycare.common.persistence.Page;
import com.sokal.easycare.common.service.CrudService;
import com.sokal.easycare.common.utils.StringUtils;
import com.sokal.easycare.common.utils.FastDFS.FastDFSClient;
import com.sokal.easycare.modules.kpoint.dao.activity.KPointActivityDao;
import com.sokal.easycare.modules.kpoint.entity.activity.KPointActivity;

/**
 * 体验店活动Service
 * @author lihaipei
 * @version 2017-03-18
 */
@Service("kPointActivityService")
@Transactional(readOnly = true)
public class KPointActivityService extends CrudService<KPointActivityDao, KPointActivity> {
	
	public KPointActivity get(String id) {
		KPointActivity kPointActivity = super.get(id);
		return kPointActivity;
	}
	
	public List<KPointActivity> findList(KPointActivity kPointActivity) {
		return super.findList(kPointActivity);
	}
	
	public Page<KPointActivity> findPage(Page<KPointActivity> page, KPointActivity kPointActivity) {
		return super.findPage(page, kPointActivity);
	}
	
	@Transactional(readOnly = false)
	public void save(MultipartFile activityFile, KPointActivity kPointActivity) {
		try {
			if (StringUtils.isNotBlank(activityFile.getOriginalFilename())) {
				kPointActivity.setImageUrl(FastDFSClient.uploadFile(activityFile, activityFile.getOriginalFilename()));
			}
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Long currTime = sdf.parse(sdf.format(new Date())).getTime();
			Long endTime = kPointActivity.getEndTime().getTime();
			Long startTime = kPointActivity.getStartTime().getTime();
			if (endTime < currTime) {
				kPointActivity.setStatus(KPointActivity.STATUS_END);
			} else if (currTime < startTime) {
				kPointActivity.setStatus(KPointActivity.STATUS_NOT_OPEN);
			} else if (startTime <= currTime && currTime <= endTime) {
				kPointActivity.setStatus(KPointActivity.STATUS_ONGOING);
			}
			super.save(kPointActivity);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	@Transactional(readOnly = false)
	public void delete(KPointActivity kPointActivity) {
		super.delete(kPointActivity);
	}
	
	@Transactional(readOnly = false)
	public void updateStatus() {
		try {
			List<KPointActivity> activities = dao.getByStatus(new KPointActivity());
			if (!activities.isEmpty()) {
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				Long currTime = sdf.parse(sdf.format(new Date())).getTime();
				for (KPointActivity activity : activities) {
					if(KPointActivity.STATUS_ONGOING.equals(activity.getStatus())){
						Long endTime = activity.getEndTime().getTime();
						if(endTime<currTime&&!KPointActivity.STATUS_END.equals(activity.getStatus())){
							activity.setStatus(KPointActivity.STATUS_END);
							super.save(activity);
						}
					}else if(KPointActivity.STATUS_NOT_OPEN.equals(activity.getStatus())){
						Long startTime = activity.getStartTime().getTime();
						if(startTime<=currTime&&!KPointActivity.STATUS_ONGOING.equals(activity.getStatus())){
							activity.setStatus(KPointActivity.STATUS_ONGOING);
							super.save(activity);
						}
						
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}